package com.example.myapplication

import android.app.Application
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import com.alibaba.android.arouter.launcher.ARouter
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.example.myapplication.ad.TTAdManagerHolder
import com.example.myapplication.viewmodel.AppViewModel
import com.shuyu.gsyvideoplayer.player.PlayerFactory
import com.tencent.mmkv.MMKV
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager

//全局ViewModel，里面存放了一些基本配置等信息
val appViewModel: AppViewModel by lazy { App.appViewModelInstance }

class App : Application(), ViewModelStoreOwner {

    companion object {
        lateinit var context: App
        lateinit var appViewModelInstance: AppViewModel
//        var isDebug = BuildConfig.DEBUG
    }


    override fun onCreate() {
        super.onCreate()
        context = this

        ARouter.init(this)
        System.loadLibrary("msaoaidsec")
        MMKV.initialize(context, filesDir.absolutePath + "/mmkv")
        PlayerFactory.setPlayManager(Exo2PlayerManager::class.java) //EXO模式
        // 使用 AndroidViewModelFactory（适用于 Application 参数的 ViewModel）
        appViewModelInstance = ViewModelProvider.AndroidViewModelFactory.getInstance(this)
            .create(AppViewModel::class.java)
    }

    override val viewModelStore: ViewModelStore
        get() = ViewModelStore()

    fun initAdSdk(listener: TTAdManagerHolder.OnAdInitListener? = null) {
        TTAdManagerHolder.init(this, listener)
        TTAdManagerHolder.start(this)
    }
}